package com.hou.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("string")
public class StringController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/keys")
    public Set<String> getAllKey(){
        Long name = stringRedisTemplate.opsForValue().size("name"); //求value长度
        return stringRedisTemplate.keys("*"); //获取所有key
    }

    @GetMapping("/ops/{key}")
    public String operateValueByKey(@PathVariable("key") String key){
        Long name = stringRedisTemplate.opsForValue().size(key); //求value长度
        stringRedisTemplate.opsForValue().append(key,"是个"); //末位追加
        stringRedisTemplate.opsForValue().setIfAbsent(key,"李世彪");//不存在时才会set,redis set操作默认会覆盖存在的value
        stringRedisTemplate.opsForValue().setIfPresent(key,"500");//存在时set
        stringRedisTemplate.opsForValue().set(key,"哈哈哈");
        return stringRedisTemplate.opsForValue().get(key); //获取所有key
    }
}
